<?php
 include "counter2.php";
 if (!$days = $counter->get_input('days', 'number', 'get')) {
     $days = 60;
 }

pagestart();
print "<html>"; ?>
<?php pagehead(_("Machine statistics")) ?>
<body bgcolor="white">
<?php pagetop(_("Machine statistics")) ?>
<?php print _("The data below is based on the currently available data from the ") ?>
<a href="/scripts/"><?php print _("machine-update scripts") ?></a> <?php print _(" that are run by a lot of machines.") ?>
<p>
<?php print _("Only data from machines that have updated within the last ") ?>
<?php echo $days ?> <?php print _(" days is included.") ?>
<?php
function presentquery($query, $total, $limit=1) {
    global $days;
    while ($arr = mysql_fetch_row($query)) {
        if ($arr[0] <= $limit) {
            $leftovers += $limit;
            continue;
        }
        $ver = $arr[1];
?>
          <tr>
              <td><a href="/reports/systemstats.php?days=<?php echo $days ?>&dig=<?php echo $ver ?>"><?php echo htmlspecialchars($ver) ?></a></td>
              <td><?php echo htmlspecialchars($arr[0]) ?></td>
              <td><?php printf("%5.1f", $arr[0] * 100 / $total) ?>%</td>
          </tr>
<?php
    }
    if ($leftovers) {
        $ver = _("Others");
?>
          <tr>
              <td><a href="/reports/systemstats.php?days=<?php echo $days ?>&dig=%"><?php print htmlspecialchars("$ver") ?></a></td>
              <td><?php $leftovers ?></td>
              <td><?php printf("%5.1f", $leftovers * 100 / $total) ?>%</td>
          </tr>
<?php
    }
}
?>
<h2><?php print _("Kernel versions") ?></h2>
<?php
  $to_days = 'WHERE TO_DAYS(updated) > TO_DAYS(NOW()) - '. $days;
  $q1 = mysql_query('SELECT COUNT(*) FROM automach '. $to_days);
  if ($q1 == 0) {
      print _("MySQL error") . ": ". mysql_errno() .":". mysql_error();
      die('Query for count failed');
  }
  $tmp = mysql_fetch_row($q1);
  $total = $tmp[0];
?>
<?php print _("Information from ") ?><?php echo $total ?><?php print _(" machines. Kernels run by only 1 machine are not listed.") ?>
<?php
  $query = mysql_query('SELECT COUNT(*) AS c, SUBSTRING_INDEX(kernel, \'-\', 1) AS k FROM automach '. $to_days .' GROUP BY k');
  if ($query == 0) {
      print _("MySQL error") . ": ". mysql_errno() .":". mysql_error();
      die('Query failed');
  }
?>
<table class="info">
   <tr>
       <td valign="top">
       <table border>
          <tr>
              <th><?php print _("Kernel") ?></th>
              <th><?php print _("Count") ?></th>
              <th><?php print _("Percentage") ?></th>
          </tr>
<?php
  presentquery($query, $total);
?>
       </table>
<?php
  $counter->add_filter('kernel', '/^([A-Z\-a-z\_0-9\.%]+)$/');
  if ($dig = $counter->get_input('dig', 'kernel', 'get')) {
      $query = mysql_query('SELECT COUNT(*) AS c, kernel AS k FROM automach '. $to_days.' AND kernel LIKE \''. $dig .'%\' GROUP BY k');
      $limit = 0;
  } else {
      $query = mysql_query('SELECT COUNT(*) AS c, SUBSTRING_INDEX(kernel, \'.\', 2) AS k FROM automach '. $to_days .' GROUP BY k');
      $limit = 1;
  }
  if ($query == 0) {
      print _("MySQL error") . ": ". mysql_errno() .":". mysql_error();
      die('Query failed');
  }
?>
       </td>
       <td valign="top">
       <table border>
<?php
  if ($dig) {
?>
          <tr>
              <th colspan="3"><?php print _("Specifics for ") ?><?php echo $dig ?></th>
          </tr>
<?php
  }
?>
          <tr>
              <th><?php print _("Kernel") ?></th>
              <th><?php print _("Count") ?></th>
              <th><?php print _("Percentage") ?></th>
          </tr>
<?php
  presentquery($query, $total, $limit);
?>
       </table>
       </td>
   </tr>
</table>
<?php pagebottom() ?>
</body>
</html>
